<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="search" class="hide">
        <el-input v-model="queryParams.search" placeholder="UID、昵称、电话号码" clearable class="search" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出金审批" prop="status">
        <el-select v-model="queryParams.status" placeholder="出金审批" clearable class="search">
          <el-option v-for="dict in statusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" class="hide">
        <el-date-picker v-model="dateRange" style="width: 240px" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item class="hide">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="screenW>700">

      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-download" size="mini" @click="exportExcel" v-hasPermi="['publicBill:auditing:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="list" stripe border>
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="创建时间" prop="createTime" width="160" align="center" sortable />
      <el-table-column label="出金审批状态" prop="status" align="center" width="120">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status == 0"> {{selectDictLabel(status,scope.row.status)}}</el-tag>
          <span v-else>{{selectDictLabel(status,scope.row.status)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="UID" prop="userId" width="80" align="center" />
      <el-table-column label="昵称" prop="userNickName" width="90" align="center" />
      <el-table-column label="用户手机" prop="phone" width="110" align="center" />
      <el-table-column label="出账(元)" prop="outMoney" width="100" align="center" sortable />
      <el-table-column label="手续费(元)" prop="withdrawCharge" width="100" align="center" />
      <el-table-column label="实付金额(元)" prop="actualAmount" width="100" align="center" />
      <el-table-column label="审核人员" prop="auditSysUserName" width="120" align="center" />
      <el-table-column label="审核时间" prop="auditTime" width="160" align="center" />
      <el-table-column label="审核说明" prop="auditNotReason" width="150" />
      <el-table-column label="对方账号" prop="oppositeAccount" width="180" align="center" />
      <el-table-column label="对方账户名" prop="oppositeAccountName" width="150" align="center" />
      <el-table-column label="对方开户行" prop="oppositeBank" width="150" align="center" />
      <el-table-column v-if="screenW>700" label="操作" align="center" class="operation" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 0 && checkPermi(['publicBill:auditing:edit']) " size="mini" type="text"  @click.stop="handleUpdate(scope.row)" >
            审核
          </el-button>
          <el-button v-if="scope.row.status == 1 && checkPermi(['publicBill:auditing:confirmOut'])" size="mini" type="text"  @click.stop="handleConfirmCashOut(scope.row)" >
            确认出金
          </el-button>
        </template>
      </el-table-column>

      <el-table-column v-else label="操作" align="center" class="operation">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 0 && checkPermi(['publicBill:auditing:edit'])" size="mini" type="text"  @click.stop="handleUpdate(scope.row)" >
            审核
          </el-button>
          <el-button v-if="scope.row.status == 1 && checkPermi(['publicBill:auditing:confirmOut'])" size="mini" type="text"  @click.stop="handleConfirmCashOut(scope.row)" >
            确认出金
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改角色配置对话框 -->
    <div v-if="open">
      <z-dialog :title="title" :width="screenW<700?'95%':'50%'" :visible.sync="open" @confirm="submitForm('fromView')" :isShowConfirm="title=='出金详情'?false:true">
        <form-view :form="form" ref="fromView"></form-view>
      </z-dialog>
    </div>

  </div>
</template>

<script>
import { listAppAdminAccOperation, updataAppAdminAccOperation, returnServiceCharge, fastOutCash,auditConfirmCashOut, againCashOut, getBusinessIdList } from "@/api/publicBill/member.js";
import formView from './formView.vue'
import { selectDictLabel } from "@/utils/ruoyi";
import { excelExportApi } from '@/api/filer'
import {checkPermi} from '@/utils/permission'

export default {
  name: "MemberBill",
  components: { formView },
  data() {
    return {
      statusTitle: '',
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息发布列表数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 日期范围
      dateRange: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },

      // 表单参数
      form: {},

      screenW: document.documentElement.clientWidth,

      status: [
        { label: '待审核', value: 0 },
        { label: '审核通过', value: 1 },
        { label: '退回用户', value: 2 },
        { label: '审核拒绝', value: 3 },
        //{ label: '出金中', value: 4 },
        { label: '出金完成', value: 5 },
       // { label: '出金失败', value: 6 },
        //{ label: '出金失败退手续费', value: 7 },
//{ label: '冻结金和出金不匹配', value: 8 },
      ]
    };
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 230px)'
    },

    statusList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.status));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },


    typeCodeList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.typeCode));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
  },
  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        this.getList();
      },
    },
    dateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.createTimeStart = String(newV[0]);
          this.queryParams.createTimeEnd = String(newV[1]);
        }
      } else {
        this.queryParams.createTimeStart = undefined;
        this.queryParams.createTimeEnd = undefined;
      }
      this.getList();

    },
  },

  created() {
    this.getList();
  },
  methods: {
    checkPermi,
    getList() {
      this.loading = true;
      listAppAdminAccOperation(this.queryParams).then((response) => {
        this.list = response.data;
        if (this.list.length) {
          this.total = response.total;
        }
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.createTimeStart = undefined;
      this.queryParams.createTimeEnd = undefined;
      this.dateRange = undefined;
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = row.status == 0 ? '出金审核' : '出金详情'
      this.form = JSON.parse(JSON.stringify(row));
      this.form.auditNotReason = '佣金提取';
      this.form.statusStr = selectDictLabel(this.status, row.status)
    },
    lookDetail(row) {
      this.reset();
      this.open = true;
      this.title = '出金详情';
      this.form = row;
      this.form.statusStr = selectDictLabel(this.status, row.status)
    },

    //重新出金
    postAgainCashOut(row) {
      getBusinessIdList({ businessId: row.actualId }).then((response) => {
        let data = response.data;
        const newDatas = []
        const h = this.$createElement

        if (data.length > 0) {
          data.forEach(item => {
            newDatas.push(h('p', null, item.createTime + (item.isOk == 1 ? '  出金失败' : '')))
          });
        }

        this.$confirm('提示', {
          title: '是否确认重新出金',
          message: h('div', null, newDatas),
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showClose: false,
          center: true
        }).then(() => {
          againCashOut({ id: row.id }).then((response) => {
            this.$modal.msgSuccess("操作成功");
            this.getList();
          }).finally(res => {
            this.open = false;
          });
        });
      }).finally(res => {
        this.open = false;
      });
    },

    /** 审核操作 */
    submitForm(ref) {
      let params = this.$refs[ref].params;
      params.remark = this.$refs[ref].form.auditNotReason;

      // this.$refs[ref].$refs.form.validate((valid) => {
      //   if (valid) {
      //     console.log(params);
      //     return;


      updataAppAdminAccOperation(params).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).finally(res => {
        this.open = false;
      });
    },
    
    /** 确认出金 */
    handleConfirmCashOut(data) {
      let params = {"id":data.id};

      auditConfirmCashOut(params).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      }).finally(res => {
        this.open = false;
      });
    },
    
    /** 导出按钮操作 */
    exportExcel() {
      this.queryParams.beanName = 'AppAdminCashOutAuditExport',
        this.queryParams.fileName = '出金审核记录',
        this.$alert('需要导出 "' + this.total + '" 条数据,是否确认导出', '提示', {
          confirmButtonText: '确定',
        }).then(() => {
          excelExportApi(this.queryParams, '/ums');
        });
    },
  },
};
</script>
<style scoped>
.search {
  width: 240px;
}
/* 响应式布局 */
@media screen and (min-width: 1280px) {
}
@media screen and (max-width: 800px) {
  .search {
    width: 100px;
  }
  .hide {
    display: none;
    width: 0;
  }
  .operation {
    width: 0;
  }
}
</style>
